﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    public class ClsKanal
    {
        public int Id { get; private set; }
        public double kapacitet { get; private set; }
        public double Vandstand { get; set; }
        public DateTime Dato { get; private set; }
        
        public List<ClsPumpe> Fra { get; set; }
        public List<ClsPumpe> Til { get; set; }

        public ClsKanal(int Id, double Kapacitet)
        {
            this.Id = Id;
            this.kapacitet = Kapacitet;
            this.Fra = new List<ClsPumpe>();
            this.Til = new List<ClsPumpe>();
        }

        public void TilføjFraPumpe(ClsPumpe Pumpe)
        {
            this.Fra.Add(Pumpe);
        }

        public void TilføjTilPumpe(ClsPumpe Pumpe)
        {
            this.Til.Add(Pumpe);
        }

        public double BeregnVandstand()
        {
            int N = 0;
            if (this.Dato != null)
                //
                N = (int)Math.Round((DateTime.Now - this.Dato).TotalSeconds);
            else
                N = 1;

            for (int i = 0; i <= N; i++)
            { 
                this.Vandstand -= this.Fra.Where(val => val.On && val.Defekt == false).Sum(val => val.KvadratmeterVandSekundet);
                this.Vandstand += this.Til.Where(val => val.On && val.Defekt == false).Sum(val => val.KvadratmeterVandSekundet);
            }

            this.Dato = DateTime.Now;
            if (this.Vandstand < 0)
                this.Vandstand = 0;

            return this.Vandstand;
        }
    }
}
